<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('index');
	}
	
	
	public function processLogin()
	{
		$this->form_validation->set_rules('username','username','trim|required|valid_email'); 
		$this->form_validation->set_rules('password','password','trim|required|min_length[5]|max_length[20]'); 

		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('index');
		}
		else
		{
			$email = $this->input->post('username');
			$password = $this->input->post('password');

			$data = array('email' => $email,
					      'password' =>$password);			
			$this->load->model('user_model');
			
			$row = $this->user_model->login_user($data);
			
			if ($row)
			{
			   $this->session->set_userdata('user', $row->username);
			   if($row->account_type=="encoder") { redirect('home/encoder', ''); }
			   else if($row->account_type=="admin") { redirect('home/admin', ''); }
			   else if($row->account_type=="student") { redirect('home/student', ''); }
			}

			else $this->load->view('index');
		
		}

	}
	public function processSignup()
	{
		$this->load->view('verify');
	}

	public function logout()
	{
		$this->session->sess_destroy();
		redirect('home', '');
	}

	/* 
	   Function name: encoder 
	   Function description: This is loaded if the user logged on as an encoder
	*/

	public function encoder()
	{
		$this->load->view('header_login_success');
		$this->load->view('encoder_pages/encoder_login_success');
	}

	/* 
	   Function name: encoder_view_students 
	   Function description: Loads necessary views (e.g. main header, header for encoder users, user model, and also the view all students' name)
	*/

	public function encoder_view_students()
	{
		/* necessary views and models */
		$this->load->view('header_login_success');
		$this->load->view('encoder_pages/encoder_login_success_body1');
		$this->load->model('user_model');
			
		/**main view for the data table**/
		$data["data"] = $this->user_model->view_all_students();
		$this->load->view('encoder_pages/encoder_view_students', $data);
		$this->load->view('encoder_pages/encoder_login_success_footer');
	}

	/* 
	   Function name: encoder_edit_student 
	   Function description: Loads necessary views (e.g. main header, header for encoder users, user model, and also the view student's academic information)
	*/

	public function encoder_edit_student()
	{
		/* necessary views and models */
		$this->load->view('header_login_success');
		$this->load->view('encoder_pages/encoder_login_success_body1');
		$this->load->model('user_model');
			
		/* displays a selected student's academic information retrieved from the get_acad_info functiom */
		$md5 = $this->input->get('std_no');
		$stdno = $this->user_model->get_stdno($md5);
		$data['data'] = $this->user_model->get_acad_info($stdno);
		$this->load->view('encoder_pages/encoder_edit_student', $data);
		$this->load->view('encoder_pages/encoder_login_success_footer');
	}
	
	/* 
	   Function name: encoder_updateRow 
	   Function description: Updates the data of a specific subject based from the encoder's input
	*/

	public function encoder_updateRow()
	{
		$this->load->model('user_model');

		/* data retrieving */
		$course_code = $this->input->post('course_codes');
		$grade = $this->input->post('grade');
		$unit = $this->input->post('unit');
		$type = $this->input->post('type');
		$stdid = $this->input->post('std_id');
		$index = $this->input->post('value');
	
		$data = array(
           'Grade' => $grade,
           'Units' => $unit,
           'Subject_Type' => $type
        );
		
		/* retrieved data is then stored into $data array which is then passed on to user_model that updates a student acad info */
		$this->user_model->encoder_update_acad_info($course_code[$index], $data);
		
		redirect('home/encoder_edit_student?std_no='.$stdid, '');
	}

	/* 
	   Function name: encoder_deleteRow 
	   Function description: Removes a subject, along with its values from the database
	*/

	public function encoder_deleteRow()
	{
		/* data retrieving */
		$this->load->model('user_model');
		$stdid = $this->input->get('stdid');
		$rowid = $this->input->get('rowId');
	
		/* row id is passed on to the delete function */
		$this->user_model->encoder_delete_acad_info($rowid);
		redirect('home/encoder_edit_student?std_no='.$stdid, '');
	}

	public function encoder_updateRegInfo()
	{
		$this->load->model('user_model');
		$stdid = $this->input->post('std_id');
		
		$std_curriculum = $this->input->post('std_curriculum');
		$reg_adviser = $this->input->post('reg_adviser');
		$sp_adviser = $this->input->post('sp_adviser');
		$std_num = $this->input->post('std_num');

		$reg_adviser_empno = $this->user_model->get_empno($reg_adviser);
		$sp_adviser_empno = $this->user_model->get_empno($sp_adviser);

		$reg_adviser_id = $reg_adviser_empno."_".$std_num;
		$sp_adviser_id = $sp_adviser_empno."_".$std_num;

		$data = array(
			'reg_adviser_empno' => $reg_adviser_empno, 
			'reg_adviser_id' => $reg_adviser_id, 
			'sp_adviser_empno' => $sp_adviser_empno, 
			'sp_adviser_id' => $sp_adviser_id,
			'std_curriculum' => $std_curriculum,
			'std_num' => $std_num,
		//	'std_id' => $stdid
		);

		$this->user_model->encoder_update_reg_info($data);
		//echo $stdid;
		redirect('home/encoder_edit_student?std_no='.$stdid, '');
	}

	public function encoder_add_subject(){
		$this->load->model('user_model');
		$stdid = $this->input->post('std_id');
		echo $stdid;

	}

	public function admin()
	{
		$this->load->view('header_login_success');
		$this->load->view('admin_pages/admin_login_success');
	}

	public function student()
	{
		$this->load->view('header_login_success');
		$this->load->view('student_pages/student_login_success');
	}
	



}



/* End of file home.php */
/* Location: ./application/controllers/home.php */